<xsl:stylesheet version="1.1"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:math="http://exslt.org/math"
	xmlns="http://www.w3.org/2000/svg" 
	extension-element-prefixes="math">

	<xsl:import href="../types-to-classes.xslt" />
	<xsl:import href="animations.xslt" />
	
	<!-- include so it can call back out (also some shared parameters) -->
	<xsl:include href="../common.xslt" />


	<xsl:param name="humanoid-body-waist-girth-default">4</xsl:param>
	<xsl:param name="humanoid-body-mult">1</xsl:param>

	<xsl:param name="humanoid-upper-body-height-default">10</xsl:param>
	<xsl:param name="humanoid-upper-body-chest-girth-default">6</xsl:param>
	<xsl:param name="humanoid-upper-body-mult">1</xsl:param>	
	
	<xsl:param name="humanoid-lower-body-height-default">2</xsl:param>
	<xsl:param name="humanoid-lower-body-girth-default">4</xsl:param>
	<xsl:param name="humanoid-lower-body-mult">1</xsl:param>
	
	<xsl:param name="humanoid-arm-length-default">5</xsl:param>
	<xsl:param name="humanoid-arm-girth-default">2</xsl:param>
	<xsl:param name="humanoid-arm-mult">1</xsl:param>	

	<xsl:param name="humanoid-forearm-girth-default">1</xsl:param>
	<xsl:param name="humanoid-forearm-length-default">3</xsl:param>
	<xsl:param name="humanoid-forearm-mult">1</xsl:param>	

	<xsl:param name="humanoid-hand-wrist-girth-default">1</xsl:param>
	<xsl:param name="humanoid-hand-length-default">1</xsl:param>
	<xsl:param name="humanoid-hand-girth-default">1.5</xsl:param>
	<xsl:param name="humanoid-hand-mult">1.2</xsl:param>	

	<xsl:param name="humanoid-finger-length-default">1.3</xsl:param>
	<xsl:param name="humanoid-finger-girth-default">0.3</xsl:param>
	<xsl:param name="humanoid-finger-mult">1.2</xsl:param>	
	<xsl:param name="humanoid-finger-number">4</xsl:param>	
	<xsl:param name="humanoid-finger-splay">30</xsl:param>	
	<xsl:param name="humanoid-finger-socket-prefix">digit_</xsl:param>	

	<xsl:param name="humanoid-thumb-splay">-45</xsl:param>	

	<xsl:param name="humanoid-leg-girth-default">2</xsl:param>
	<xsl:param name="humanoid-leg-length-default">5</xsl:param>
	<xsl:param name="humanoid-leg-mult">1</xsl:param>	

	<xsl:param name="humanoid-calf-girth-default">1.5</xsl:param>
	<xsl:param name="humanoid-calf-length-default">4</xsl:param>
	<xsl:param name="humanoid-calf-mult">1</xsl:param>	
	
	<xsl:param name="humanoid-foot-ankle-girth-default">1</xsl:param>
	<xsl:param name="humanoid-foot-length-default">2</xsl:param>
	<xsl:param name="humanoid-foot-height-default">1</xsl:param>
	<xsl:param name="humanoid-foot-mult">1.2</xsl:param>

	<xsl:param name="humanoid-neck-girth-default">1.5</xsl:param>
	<xsl:param name="humanoid-neck-length-default">2</xsl:param>
	<xsl:param name="humanoid-neck-mult">1</xsl:param>	

	<xsl:param name="humanoid-head-width-default">3.5</xsl:param>
	<xsl:param name="humanoid-head-length-default">4</xsl:param>
	<xsl:param name="humanoid-head-mult">1.2</xsl:param>	
	
	<!-- humanoid -->	
	<xsl:template match="part[type//@name='humanoid']" mode="render">
		<xsl:param name="state"><xsl:value-of select="@state"/></xsl:param>
		<xsl:param name="orientation"/>
		<xsl:message>state:<xsl:value-of select="$state"/></xsl:message>
		<g>
			<g>
 				<xsl:apply-templates select="part" mode="render">
 					<xsl:with-param name="state" select="$state"/>
 					<xsl:with-param name="orientation" select="$orientation"/>
 				</xsl:apply-templates>
 			</g>
		</g>
		
	</xsl:template>

	<xsl:template 
		match="part[type//@name='humanoid']"
		mode="get-extent">
		<xsl:param name="parent-angle">0</xsl:param>
		<xsl:param name="direction"/>
		<xsl:param name="state"/>
		<!-- let children handle this --> 
		<xsl:apply-templates select="part" mode="get-extent">
			<xsl:with-param name="parent-angle" select="$parent-angle"/>
			<xsl:with-param name="orientation" select="$orientation"/>
			<xsl:with-param name="direction" select="$direction"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<!-- humanoid body (does nothing) except delegate to children (and animate) -->
	<xsl:template match="part[type//@name='humanoid-body']" mode="render">
		<xsl:param name="state"/>
		<xsl:param name="orientation"/>
		<g>
			<xsl:apply-templates select="part" mode="render">
				<xsl:with-param name="state" select="$state"/>
				<xsl:with-param name="orientation" select="$orientation"/>
			</xsl:apply-templates>
			<xsl:apply-templates select="." mode="animate">
				<xsl:with-param name="state" select="$state"/>
			</xsl:apply-templates>			
			
		</g>
	</xsl:template>

	<xsl:template match="part[type//@name='humanoid-body']" mode="get-extent">
		<xsl:param name="direction"/>
		<xsl:param name="state"/>
		<xsl:param name="orientation"/>
		<xsl:choose>
			<xsl:when test="$direction='grabx'">
				<xsl:text>0</xsl:text>
			</xsl:when>
			<xsl:when test="$direction='graby'">
				<!-- lower body extent -->
				<xsl:apply-templates select="part[@socket='bottom']" mode="get-extent">
					<xsl:with-param name="direction" select="$direction"/>
					<xsl:with-param name="state" select="$state"/>
					<xsl:with-param name="orientation" select="$orientation"/>
				</xsl:apply-templates>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="." mode="get-max-child-extent">
					<xsl:with-param name="direction" select="$direction"/>
					<xsl:with-param name="state" select="$state"/>
					<xsl:with-param name="orientation" select="$orientation"/>
				</xsl:apply-templates>
			</xsl:otherwise>
		</xsl:choose>
				
	</xsl:template>
	 	
	<!-- generic skewing for oblique parts -->
	<xsl:template match="part[type//@name='humanoid-arm' or type//@name='humanoid-leg' or type//@name='humanoid-forearm' or type//@name='humanoid-hand' or type//@name='humanoid-finger' or type//@name='humanoid-calf' or type//@name='humanoid-foot']" mode="render">
		<xsl:param name="orientation"/>
		<xsl:param name="state"/>
		<xsl:param name="rotation">0</xsl:param>
		
		<xsl:call-template name="render-skewed">
			<xsl:with-param name="orientation" select="$orientation"/>
			<xsl:with-param name="state" select="$state"/>
			<xsl:with-param name="rotation" select="$rotation"/>
			<xsl:with-param name="part" select="."/>
		</xsl:call-template>

	</xsl:template>	
	
	<!-- generic skewing for complimented parts -->
	<xsl:template match="part[type//@name='humanoid-upper-body' or type//@name='humanoid-lower-body' or type//@name='humanoid-neck']" mode="render">
		<xsl:param name="orientation"/>
		<xsl:param name="state"/>
		<xsl:param name="rotation">0</xsl:param>
		
		<xsl:call-template name="render-skewed-compliment">
			<xsl:with-param name="orientation" select="$orientation"/>
			<xsl:with-param name="state" select="$state"/>
			<xsl:with-param name="rotation" select="$rotation"/>
			<xsl:with-param name="part" select="."/>
		</xsl:call-template>

	</xsl:template>
	
	<!-- upper body -->
	<xsl:template match="part[type//@name='humanoid-upper-body']" mode="render-skewed-part">
		<xsl:param name="state"/>
		<xsl:param name="face"/>
		<xsl:param name="skewYAngle">0</xsl:param>
		<xsl:param name="scaleX">1</xsl:param>
		<xsl:param name="orientation"/>
		
		<xsl:variable name="height">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-upper-body-height-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">height</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-upper-body-mult"/>
			</xsl:call-template>
		</xsl:variable>
		
		<xsl:variable name="chest-girth">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-upper-body-chest-girth-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">chest-girth</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-upper-body-mult"/>
			</xsl:call-template>
		</xsl:variable>

		<xsl:variable name="waist-girth">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-body-waist-girth-default"/>
				<xsl:with-param name="partNode" select=".."/>
				<xsl:with-param name="key">waist-girth</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-body-mult"/>
			</xsl:call-template>
		</xsl:variable>
		
		<xsl:variable name="neck-girth">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-neck-girth-default"/>
				<xsl:with-param name="partNode" select="part[@socket='collar']"/>
				<xsl:with-param name="key">girth</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-neck-mult"/>
			</xsl:call-template>
		</xsl:variable>
		
		<g>
			<g transform="translate({$chest-girth div 2},{-$height + $humanoid-arm-girth-default div 2})">
				<g transform="scale({1 div $scaleX},1) skewY({-$skewYAngle})">
					<!-- back shoulder -->
					<xsl:variable name="back-shoulder">
						<xsl:choose>
							<xsl:when test="$orientation='northEast' or $orientation='southEast'">left-shoulder</xsl:when>
							<xsl:otherwise>right-shoulder</xsl:otherwise>
						</xsl:choose>
					</xsl:variable>
					<!--  TODO probably should be call template to avoid having to match types? -->
					<xsl:apply-templates select="part[@socket=$back-shoulder]" mode="render">
						<xsl:with-param name="state" select="$state"/>
						<xsl:with-param name="face" select="'inner'"/>
						<xsl:with-param name="orientation" select="$orientation"/>
						<xsl:with-param name="rotation" select="90"/>
					</xsl:apply-templates>
				</g>
			</g>
			
			<polygon points="{-$chest-girth div 2},{-$height + $humanoid-arm-girth-default div 4} {-$neck-girth div 2},{-$height} {$neck-girth div 2},{-$height} {$chest-girth div 2},{-$height + $humanoid-arm-girth-default div 4} {$waist-girth div 2},0 {-$waist-girth div 2},0" fill="black">
				<xsl:attribute name="class">
			        <xsl:apply-templates select="type" mode="to-classes">
					  <xsl:with-param name="postfix">-<xsl:value-of select="@material"/>-<xsl:value-of select="$face"/></xsl:with-param>
					</xsl:apply-templates>
				</xsl:attribute>
			</polygon>
	
			<g transform="translate(0,{-$height})">
				<g transform="scale({1 div $scaleX},1) skewY({-$skewYAngle})">
					<!-- head/neck -->
					<xsl:apply-templates select="part[@socket='collar']" mode="render">
						<xsl:with-param name="state" select="$state"/>
						<xsl:with-param name="face" select="'outer'"/>
						<xsl:with-param name="orientation" select="$orientation"/>
						<xsl:with-param name="rotation" select="0"/>
					</xsl:apply-templates>
				</g>
			</g>
	
			<g transform="translate({-$chest-girth div 2},{-$height + $humanoid-arm-girth-default div 2})">
				<g transform="scale({1 div $scaleX},1) skewY({-$skewYAngle})">
					
					<!-- front shoulder -->
					<xsl:variable name="front-shoulder">
						<xsl:choose>
							<xsl:when test="$orientation='northEast' or $orientation='southEast'">right-shoulder</xsl:when>
							<xsl:otherwise>left-shoulder</xsl:otherwise>
						</xsl:choose>
					</xsl:variable>
					<xsl:apply-templates select="part[@socket=$front-shoulder]" mode="render">
						<xsl:with-param name="state" select="$state"/>
						<xsl:with-param name="face" select="'outer'"/>
						<xsl:with-param name="orientation" select="$orientation"/>
						<xsl:with-param name="rotation" select="90"/>
						<xsl:with-param name="socket" select="$front-shoulder"/>					
					</xsl:apply-templates>
				</g>
			</g>
			
		</g>		
	</xsl:template>	

	<xsl:template match="part[type//@name='humanoid-upper-body']" mode="get-extent">
		<xsl:param name="direction"/>
		<xsl:param name="state"/>
		<xsl:param name="orientation"/>
		<xsl:param name="rotation">0</xsl:param>
		
		<xsl:variable name="height">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-upper-body-height-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">height</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-upper-body-mult"/>
			</xsl:call-template>
		</xsl:variable>
		
		<xsl:variable name="chest-girth">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-upper-body-chest-girth-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">chest-girth</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-upper-body-mult"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:variable name="scaled-chest-width" select="$chest-girth * $complimentScaleX"/>
		<xsl:variable name="max-y" select="$height + math:tan($complimentSkewY * $degrees-to-radians) * $chest-girth"/>
		
		<xsl:variable name="neck-extent">
			<xsl:apply-templates select="part[@socket='collar']" mode="get-extent">
				<xsl:with-param name="state" select="$state"/>
				<xsl:with-param name="orientation" select="$orientation"/>
				<xsl:with-param name="direction" select="$direction"/>
			</xsl:apply-templates>
		</xsl:variable>

		<!-- expect the arms to be swinging everywhere so ignore direction -->
		<xsl:variable name="left-arm-extent">
			<xsl:apply-templates select="part[@socket='left-shoulder']" mode="get-extent">
				<xsl:with-param name="state" select="$state"/>
				<xsl:with-param name="orientation" select="$orientation"/>
			</xsl:apply-templates>
		</xsl:variable>
		<xsl:variable name="right-arm-extent">
			<xsl:apply-templates select="part[@socket='right-shoulder']" mode="get-extent">
				<xsl:with-param name="state" select="$state"/>
				<xsl:with-param name="orientation" select="$orientation"/>
			</xsl:apply-templates>
		</xsl:variable>
		
		<xsl:variable name="max-arm-extent">
			<xsl:choose>
				<xsl:when test="$left-arm-extent > $right-arm-extent">
					<xsl:value-of select="$left-arm-extent"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$right-arm-extent"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		
		<!-- TODO there will be issues with a rotated/prostrate person, but let's igonre that (use no direction in that case?) -->
		<xsl:choose>
			<xsl:when test="$direction='east'">
				<!-- not entirely correct, but it will do (should work out which arm is forward and which is back) -->
				<xsl:value-of select="$scaled-chest-width div 2 + $max-arm-extent * $obliqueScaleX"/>
			</xsl:when>
			<xsl:when test="$direction='west'">
				<!-- not entirely correct, but it will do (should work out which arm is forward and which is back) -->
				<xsl:value-of select="-$scaled-chest-width div 2 - $max-arm-extent * $obliqueScaleX"/>
			</xsl:when>
			<xsl:when test="$direction='south'">
				<xsl:choose>
					<xsl:when test="$max-arm-extent > $height">
						<!-- TODO may underreport the height (arms are slightly lower down) -->
						<xsl:value-of select="$max-arm-extent - $height"></xsl:value-of>
					</xsl:when>
					<xsl:otherwise>0</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:when test="$direction='north'">
				<xsl:choose>
					<xsl:when test="-$neck-extent > $max-arm-extent">
						<xsl:value-of select="-$height + $neck-extent"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="-($height + $max-arm-extent)"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:when test="$direction='grabx'">
				<xsl:text>0</xsl:text>
			</xsl:when>
			<xsl:when test="$direction='graby'">
				<xsl:text>0</xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:choose>
					<xsl:when test="-$neck-extent > $max-arm-extent">
						<xsl:value-of select="$height - $neck-extent"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$height + $max-arm-extent"/>
					</xsl:otherwise>
				</xsl:choose>				
			</xsl:otherwise>
		</xsl:choose>
				
	</xsl:template>
	

	<!-- lower body -->
	<xsl:template match="part[type//@name='humanoid-lower-body']" mode="render-skewed-part">
		<xsl:param name="state"/>
		<xsl:param name="face"/>
		<xsl:param name="skewYAngle">0</xsl:param>
		<xsl:param name="scaleX">1</xsl:param>
		<xsl:param name="orientation"/>
		
		<xsl:variable name="waist-girth">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-body-waist-girth-default"/>
				<xsl:with-param name="partNode" select=".."/>
				<xsl:with-param name="key">waist-girth</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-body-mult"/>
			</xsl:call-template>
		</xsl:variable>
		
		<xsl:variable name="girth">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-lower-body-girth-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">girth</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-lower-body-mult"/>
			</xsl:call-template>
		</xsl:variable>
		
		<xsl:variable name="height">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-lower-body-height-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">height</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-lower-body-mult"/>
			</xsl:call-template>
		</xsl:variable>
		
		
		<g transform="translate({$girth div 4},{$height * 0.75})">
			<g transform="scale({1 div $scaleX},1) skewY({-$skewYAngle})">
				<!-- back hip -->
				<xsl:variable name="back-hip">
					<xsl:choose>
						<xsl:when test="$orientation='northEast' or $orientation='southEast'">left-hip</xsl:when>
						<xsl:otherwise>right-hip</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>
				<xsl:apply-templates select="part[@socket=$back-hip]" mode="render">
					<xsl:with-param name="state" select="$state"/>
					<xsl:with-param name="face" select="'inner'"/>
					<xsl:with-param name="orientation" select="$orientation"/>
					<xsl:with-param name="rotation" select="90"/>
				</xsl:apply-templates>
			</g>
		</g>
		<polygon points="{-$waist-girth div 2},0 {$waist-girth div 2},0 {$girth div 2},{$height div 2} {$girth div 4},{$height} {-$girth div 4},{$height} {-$girth div 2},{$height div 2}">
			<xsl:attribute name="class">
		        <xsl:apply-templates select="type" mode="to-classes">
				  <xsl:with-param name="postfix">-<xsl:value-of select="@material"/>-<xsl:value-of select="$face"/></xsl:with-param>
				</xsl:apply-templates>
			</xsl:attribute>
		</polygon>
		<g transform="translate({-$girth div 4},{$height * 0.75})">
			<g transform="scale({1 div $scaleX},1) skewY({-$skewYAngle})">
				<!-- front hip -->
				<xsl:variable name="front-hip">
					<xsl:choose>
						<xsl:when test="$orientation='northEast' or $orientation='southEast'">right-hip</xsl:when>
						<xsl:otherwise>left-hip</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>
				<xsl:apply-templates select="part[@socket=$front-hip]" mode="render">
					<xsl:with-param name="state" select="$state"/>
					<xsl:with-param name="face" select="'outer'"/>
					<xsl:with-param name="orientation" select="$orientation"/>
					<xsl:with-param name="rotation" select="90"/>
				</xsl:apply-templates>
			</g>
		</g>
	</xsl:template>	
	
	<xsl:template match="part[type//@name='humanoid-lower-body']" mode="get-extent">
		<xsl:param name="direction"/>
		<xsl:param name="state"/>
		<xsl:param name="orientation"/>
		
		<xsl:variable name="waist-girth">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-body-waist-girth-default"/>
				<xsl:with-param name="partNode" select=".."/>
				<xsl:with-param name="key">waist-girth</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-body-mult"/>
			</xsl:call-template>
		</xsl:variable>
		
		<xsl:variable name="girth">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-lower-body-girth-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">girth</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-lower-body-mult"/>
			</xsl:call-template>
		</xsl:variable>
		
		<xsl:variable name="max-girth">
			<xsl:choose>
				<xsl:when test="$girth > $waist-girth">
					<xsl:value-of select="$girth"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$waist-girth"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		
		<xsl:variable name="scaled-max-girth" select="$max-girth * $complimentScaleX div 2"/>
		
		<xsl:variable name="height">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-lower-body-height-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">height</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-lower-body-mult"/>
			</xsl:call-template>
		</xsl:variable>
		
		<!-- expect the legs to be swinging everywhere so ignore direction -->
		<xsl:variable name="left-leg-extent">
			<xsl:apply-templates select="part[@socket='left-hip']" mode="get-extent">
				<xsl:with-param name="state" select="$state"/>
				<xsl:with-param name="orientation" select="$orientation"/>
			</xsl:apply-templates>
		</xsl:variable>
		<xsl:variable name="right-leg-extent">
			<xsl:apply-templates select="part[@socket='right-hip']" mode="get-extent">
				<xsl:with-param name="state" select="$state"/>
				<xsl:with-param name="orientation" select="$orientation"/>
			</xsl:apply-templates>
		</xsl:variable>
		
		<xsl:variable name="max-leg-extent">
			<xsl:choose>
				<xsl:when test="$right-leg-extent > $left-leg-extent">
					<xsl:value-of select="$right-leg-extent"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$left-leg-extent"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		
		<xsl:choose>
			<xsl:when test="$direction='west'">
				<xsl:choose>
					<xsl:when test="$scaled-max-girth > $max-leg-extent">
						<xsl:value-of select="-$scaled-max-girth"/>
					</xsl:when>
					<xsl:otherwise>
						<!-- TODO should probably have half the hip width thrown in -->
						<xsl:value-of select="-$max-leg-extent"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:when test="$direction='east'">
				<xsl:choose>
					<xsl:when test="$scaled-max-girth > $max-leg-extent">
						<xsl:value-of select="$scaled-max-girth"/>
					</xsl:when>
					<xsl:otherwise>
						<!-- TODO should probably have half the hip width thrown in -->
						<xsl:value-of select="$max-leg-extent"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:when test="$direction='north'">0</xsl:when>
			<xsl:when test="$direction='grabx'">0</xsl:when>
			<xsl:when test="$direction='graby'">
				<!--  average of legs grabys -->
				<xsl:variable name="left-leg-graby">
					<xsl:apply-templates select="part[@socket='left-hip']" mode="get-extent">
						<xsl:with-param name="state" select="$state"/>
						<xsl:with-param name="orientation" select="$orientation"/>
						<xsl:with-param name="direction" select="$direction"/>
						<xsl:with-param name="rotation">90</xsl:with-param>
					</xsl:apply-templates>
				</xsl:variable>
				<xsl:variable name="right-leg-graby">
					<xsl:apply-templates select="part[@socket='right-hip']" mode="get-extent">
						<xsl:with-param name="state" select="$state"/>
						<xsl:with-param name="orientation" select="$orientation"/>
						<xsl:with-param name="direction" select="$direction"/>
						<xsl:with-param name="rotation">90</xsl:with-param>
					</xsl:apply-templates>
				</xsl:variable>
				<xsl:value-of select="($right-leg-graby + $left-leg-graby) div 2"/>				
			</xsl:when>
			<xsl:otherwise>
				<!-- south or max-extent -->
				<xsl:value-of select="$height + $max-leg-extent"/>			
			</xsl:otherwise>			
		</xsl:choose>
		
	</xsl:template>	
	
	<!-- arm -->
	<xsl:template match="part[type//@name='humanoid-arm']" mode="render-skewed-part">
		<xsl:param name="state"/>
		<xsl:param name="face"/>
		<xsl:param name="skewYAngle">0</xsl:param>
		<xsl:param name="scaleX">1</xsl:param>
		<xsl:param name="orientation"/>
		
		<xsl:variable name="start-girth">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-arm-girth-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">girth</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-arm-mult"/>
			</xsl:call-template>
		</xsl:variable>

		<xsl:variable name="end-girth">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-forearm-girth-default"/>
				<xsl:with-param name="partNode" select="part[@socket='joint']"/>
				<xsl:with-param name="key">girth</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-forearm-mult"/>
			</xsl:call-template>
		</xsl:variable>
		
		<xsl:variable name="length">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-arm-length-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">length</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-arm-mult"/>
			</xsl:call-template>
		</xsl:variable>
		
		<g>
			<path 
				d="	M0,{$start-girth div 2} 
					L{$length},{$end-girth div 2}
					C{$length + $end-girth div 2},{$end-girth div 2} {$length + $end-girth div 2},{-$end-girth div 2} {$length},{-$end-girth div 2}
					L0,{-$start-girth div 2}
					C{-$start-girth div 2},{-$start-girth div 2} {-$start-girth div 2},{$start-girth div 2} 0,{$start-girth div 2}">
				<xsl:attribute name="class">
			        <xsl:apply-templates select="type" mode="to-classes">
					  <xsl:with-param name="postfix">-<xsl:value-of select="@material"/>-<xsl:value-of select="$face"/></xsl:with-param>
					</xsl:apply-templates>
				</xsl:attribute>
			</path>
			<g transform="translate({$length},0)">
				<g transform="scale({1 div $scaleX},1) skewY({-$skewYAngle})">
					<xsl:apply-templates select="part[@socket='joint']" mode="render">
						<xsl:with-param name="state" select="$state"/>
						<xsl:with-param name="face" select="$face"/>
						<xsl:with-param name="orientation" select="$orientation"/>
						<xsl:with-param name="rotation" select="0"/>
					</xsl:apply-templates>
				</g>
			</g>
			<xsl:apply-templates select="." mode="animate">
				<xsl:with-param name="state" select="$state"/>
			</xsl:apply-templates>			
		</g>
	</xsl:template>
	
	<xsl:template match="part[type//@name='humanoid-arm']" mode="get-extent">
		<xsl:param name="direction"/>
		<xsl:param name="state"/>
		<xsl:param name="orientation"/>
		<xsl:param name="rotation">0</xsl:param>

		<xsl:apply-templates select="." mode="get-extent-of-socketed-length">
			<xsl:with-param name="direction" select="$direction"/>
			<xsl:with-param name="state" select="$state"/>
			<xsl:with-param name="orientation" select="$orientation"/>
			<xsl:with-param name="rotation" select="$rotation"/>
			<xsl:with-param name="default" select="$humanoid-arm-length-default"/>
			<xsl:with-param name="mult" select="$humanoid-arm-mult"/>
		</xsl:apply-templates>
	</xsl:template>	

	<!-- forearm -->
	<xsl:template match="part[type//@name='humanoid-forearm']" mode="render-skewed-part">
		<xsl:param name="state"/>
		<xsl:param name="face"/>
		<xsl:param name="skewYAngle">0</xsl:param>
		<xsl:param name="scaleX">1</xsl:param>
		<xsl:param name="orientation"/>
		
		<xsl:variable name="start-girth">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-forearm-girth-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">girth</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-forearm-mult"/>
			</xsl:call-template>
		</xsl:variable>

		<xsl:variable name="end-girth">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-hand-wrist-girth-default"/>
				<xsl:with-param name="partNode" select="part[@socket='joint']"/>
				<xsl:with-param name="key">wrist-girth</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-hand-mult"/>
			</xsl:call-template>
		</xsl:variable>
		
		<xsl:variable name="length">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-forearm-length-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">length</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-forearm-mult"/>
			</xsl:call-template>
		</xsl:variable>
		
		
		<g>
			<path 
				d="	M0,{$start-girth div 2} 
					L{$length},{$end-girth div 2}
					C{$length + $end-girth div 2},{$end-girth div 2} {$length + $end-girth div 2},{-$end-girth div 2} {$length},{-$end-girth div 2}
					L0,{-$start-girth div 2}
					C{-$start-girth div 2},{-$start-girth div 2} {-$start-girth div 2},{$start-girth div 2} 0,{$start-girth div 2}">
				<xsl:attribute name="class">
			        <xsl:apply-templates select="type" mode="to-classes">
					  <xsl:with-param name="postfix">-<xsl:value-of select="@material"/>-<xsl:value-of select="$face"/></xsl:with-param>
					</xsl:apply-templates>
				</xsl:attribute>
			</path>
			<g transform="translate({$length},0)">
				<g transform="scale({1 div $scaleX},1) skewY({-$skewYAngle})">
					<xsl:apply-templates select="part[@socket='joint']" mode="render">
						<xsl:with-param name="state" select="$state"/>
						<xsl:with-param name="face" select="$face"/>
						<xsl:with-param name="orientation" select="$orientation"/>
						<xsl:with-param name="rotation" select="0"/>
					</xsl:apply-templates>
				</g>
			</g>
			<xsl:apply-templates select="." mode="animate">
				<xsl:with-param name="state" select="$state"/>
			</xsl:apply-templates>						
		</g>
	</xsl:template>

	<xsl:template match="part[type//@name='humanoid-forearm']" mode="get-extent">
		<xsl:param name="direction"/>
		<xsl:param name="state"/>
		<xsl:param name="orientation"/>
		<xsl:param name="rotation">0</xsl:param>

		<xsl:apply-templates select="." mode="get-extent-of-socketed-length">
			<xsl:with-param name="direction" select="$direction"/>
			<xsl:with-param name="state" select="$state"/>
			<xsl:with-param name="orientation" select="$orientation"/>
			<xsl:with-param name="rotation" select="$rotation"/>
			<xsl:with-param name="default" select="$humanoid-forearm-length-default"/>
			<xsl:with-param name="mult" select="$humanoid-forearm-mult"/>
		</xsl:apply-templates>
	</xsl:template>	
	
	<!-- hand -->
	<xsl:template match="part[type//@name='humanoid-hand']" mode="render-skewed-part">
		<xsl:param name="state"/>
		<xsl:param name="face"/>
		<xsl:param name="skewYAngle">0</xsl:param>
		<xsl:param name="scaleX">1</xsl:param>
		<xsl:param name="orientation"/>
		
		<xsl:variable name="wrist-girth">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-hand-wrist-girth-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">wrist-girth</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-hand-mult"/>
			</xsl:call-template>
		</xsl:variable>
		
		<xsl:variable name="girth">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-hand-girth-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">girth</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-hand-mult"/>
			</xsl:call-template>
		</xsl:variable>

		<xsl:variable name="length">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-hand-length-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">length</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-hand-mult"/>
			</xsl:call-template>
		</xsl:variable>
		
		
		<polygon points="0,-{$wrist-girth div 2} 0,{$wrist-girth div 2} {$length},{$girth div 2} {$length},{-$girth div 2}">
			<xsl:attribute name="class">
		        <xsl:apply-templates select="type" mode="to-classes">
				  <xsl:with-param name="postfix">-<xsl:value-of select="@material"/>-<xsl:value-of select="$face"/></xsl:with-param>
				</xsl:apply-templates>
			</xsl:attribute>
		</polygon>
		
		<xsl:variable name="digit_1_girth">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-finger-girth-default"/>
				<xsl:with-param name="partNode" select="part[@socket='digit_1']"/>
				<xsl:with-param name="key">girth</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-finger-mult"/>
			</xsl:call-template>
		</xsl:variable>

		<xsl:variable name="digit_4_girth">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-finger-girth-default"/>
				<xsl:with-param name="partNode" select="part[@socket='digit_4']"/>
				<xsl:with-param name="key">girth</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-finger-mult"/>
			</xsl:call-template>
		</xsl:variable>
		
		<xsl:variable name="finger-girth" select="$girth - $digit_1_girth div 2 - $digit_4_girth div 2"/>
		<xsl:variable name="finger-offset" select="-($girth - $digit_1_girth) div 2"/> 
		
		<xsl:for-each select="part[starts-with(@socket, $humanoid-finger-socket-prefix)]">
			<xsl:variable name="position" select="substring(@socket, string-length($humanoid-finger-socket-prefix)+1)"/>
			<g transform="translate({$length},{$finger-offset + $finger-girth div ($humanoid-finger-number - 1) * ($position - 1)})">
				<g transform="scale({1 div $scaleX},1) skewY({-$skewYAngle})">
					<xsl:apply-templates select="." mode="render">
						<xsl:with-param name="state" select="$state"/>
						<xsl:with-param name="face" select="$face"/>
						<xsl:with-param name="orientation" select="$orientation"/>
						<xsl:with-param name="rotation" select="-$humanoid-finger-splay div 2 + $humanoid-finger-splay div ($humanoid-finger-number - 1) * ($position - 1) "/>
					</xsl:apply-templates>
				</g>
			</g>
			
		</xsl:for-each>

		<g transform="translate({$length div 2},{-($wrist-girth + $girth) div 4})">
			<g transform="scale({1 div $scaleX},1) skewY({-$skewYAngle})">
				<xsl:apply-templates select="part[@socket='thumb']" mode="render">
					<xsl:with-param name="state" select="$state"/>
					<xsl:with-param name="face" select="$face"/>
					<xsl:with-param name="orientation" select="$orientation"/>
					<xsl:with-param name="rotation" select="$humanoid-thumb-splay"/>
				</xsl:apply-templates>
			</g>
		</g>
		
	</xsl:template>
	
	<xsl:template match="part[type//@name='humanoid-hand']" mode="get-extent">
		<xsl:param name="direction"/>
		<xsl:param name="state"/>
		<xsl:param name="orientation"/>

		<xsl:variable name="length">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-hand-length-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">length</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-hand-mult"/>
			</xsl:call-template>
		</xsl:variable>
		
		<xsl:variable name="max-child-extent">
			<xsl:apply-templates select="." mode="get-max-child-extent">
				<xsl:with-param name="direction" select="$direction"/>
				<xsl:with-param name="orientation" select="$orientation"/>
				<xsl:with-param name="state" select="Sstate"/>
			</xsl:apply-templates>
		</xsl:variable>
		
		<!-- TODO use direction -->
		<xsl:value-of select="$length + $max-child-extent"/>
	</xsl:template>		

	<!-- finger -->
	<xsl:template match="part[type//@name='humanoid-finger']" mode="render-skewed-part">
		<xsl:param name="state"/>
		<xsl:param name="face"/>
		<xsl:param name="orientation"/>
		
		<xsl:variable name="girth">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-finger-girth-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">girth</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-finger-mult"/>
			</xsl:call-template>
		</xsl:variable>

		<xsl:variable name="length">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-finger-length-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">length</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-finger-mult"/>
			</xsl:call-template>
		</xsl:variable>
		
		
		<path 
			d="	M0,{$girth div 2} 
				L{$length},{$girth div 2}
				C{$length + $girth div 2},{$girth div 2} {$length + $girth div 2},{-$girth div 2} {$length},{-$girth div 2}
				L0,{-$girth div 2}
				C{-$girth div 2},{-$girth div 2} {-$girth div 2},{$girth div 2} 0,{$girth div 2}">
			<xsl:attribute name="class">
		        <xsl:apply-templates select="type" mode="to-classes">
				  <xsl:with-param name="postfix">-<xsl:value-of select="@material"/>-<xsl:value-of select="$face"/></xsl:with-param>
				</xsl:apply-templates>
			</xsl:attribute>
		</path>
	</xsl:template>

	<xsl:template match="part[type//@name='humanoid-finger']" mode="get-extent">
		<xsl:param name="direction"/>
		<xsl:param name="state"/>
		<xsl:param name="orientation"/>
		<xsl:param name="rotation">0</xsl:param>

		<xsl:apply-templates select="." mode="get-extent-of-socketed-length">
			<xsl:with-param name="direction" select="$direction"/>
			<xsl:with-param name="state" select="$state"/>
			<xsl:with-param name="orientation" select="$orientation"/>
			<xsl:with-param name="rotation" select="$rotation"/>
			<xsl:with-param name="default" select="$humanoid-finger-length-default"/>
			<xsl:with-param name="mult" select="$humanoid-finger-mult"/>
		</xsl:apply-templates>
	</xsl:template>	
	
	<!-- leg -->
	<xsl:template match="part[type//@name='humanoid-leg']" mode="render-skewed-part">
		<xsl:param name="state"/>
		<xsl:param name="face"/>
		<xsl:param name="skewYAngle">0</xsl:param>
		<xsl:param name="scaleX">1</xsl:param>
		<xsl:param name="orientation"/>
		
		<xsl:variable name="start-girth">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-leg-girth-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">girth</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-leg-mult"/>
			</xsl:call-template>
		</xsl:variable>

		<xsl:variable name="end-girth">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-calf-girth-default"/>
				<xsl:with-param name="partNode" select="part[@socket='joint']"/>
				<xsl:with-param name="key">girth</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-calf-mult"/>
			</xsl:call-template>
		</xsl:variable>

		<xsl:variable name="length">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-leg-length-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">length</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-leg-mult"/>
			</xsl:call-template>
		</xsl:variable>
		
		<g>
			<path 
				d="	M0,{$start-girth div 2} 
					L{$length},{$end-girth div 2}
					C{$length + $end-girth div 2},{$end-girth div 2} {$length + $end-girth div 2},{-$end-girth div 2} {$length},{-$end-girth div 2}
					L0,{-$start-girth div 2}
					C{-$start-girth div 2},{-$start-girth div 2} {-$start-girth div 2},{$start-girth div 2} 0,{$start-girth div 2}">
				<xsl:attribute name="class">
			        <xsl:apply-templates select="type" mode="to-classes">
					  <xsl:with-param name="postfix">-<xsl:value-of select="@material"/>-<xsl:value-of select="$face"/></xsl:with-param>
					</xsl:apply-templates>
				</xsl:attribute>
			</path>
			<g transform="translate({$length},0)">
				<g transform="scale({1 div $scaleX},1) skewY({-$skewYAngle})">
					<xsl:apply-templates select="part[@socket='joint']" mode="render">
						<xsl:with-param name="state" select="$state"/>
						<xsl:with-param name="face" select="$face"/>
						<xsl:with-param name="orientation" select="$orientation"/>
						<xsl:with-param name="rotation" select="0"/>
					</xsl:apply-templates>
				</g>
			</g>
			<xsl:apply-templates select="." mode="animate">
				<xsl:with-param name="state" select="$state"/>
			</xsl:apply-templates>						
			
		</g>		
	</xsl:template>

	<xsl:template match="part[type//@name='humanoid-leg']" mode="get-extent">
		<xsl:param name="direction"/>
		<xsl:param name="state"/>
		<xsl:param name="orientation"/>
		<xsl:param name="rotation">0</xsl:param>

		<xsl:apply-templates select="." mode="get-extent-of-socketed-length">
			<xsl:with-param name="direction" select="$direction"/>
			<xsl:with-param name="state" select="$state"/>
			<xsl:with-param name="orientation" select="$orientation"/>
			<xsl:with-param name="rotation" select="$rotation"/>
			<xsl:with-param name="default" select="$humanoid-leg-length-default"/>
			<xsl:with-param name="mult" select="$humanoid-leg-mult"/>
		</xsl:apply-templates>
	</xsl:template>	
	
	<!-- calf -->
	<xsl:template match="part[type//@name='humanoid-calf']" mode="render-skewed-part">
		<xsl:param name="state"/>
		<xsl:param name="face"/>
		<xsl:param name="skewYAngle">0</xsl:param>
		<xsl:param name="scaleX">1</xsl:param>
		<xsl:param name="orientation"/>
		
		<xsl:variable name="start-girth">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-calf-girth-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">girth</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-calf-mult"/>
			</xsl:call-template>
		</xsl:variable>

		<xsl:variable name="end-girth">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-foot-ankle-girth-default"/>
				<xsl:with-param name="partNode" select="part[@socket='joint']"/>
				<xsl:with-param name="key">ankle-girth</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-foot-mult"/>
			</xsl:call-template>
		</xsl:variable>

		<xsl:variable name="length">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-calf-length-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">length</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-calf-mult"/>
			</xsl:call-template>
		</xsl:variable>
		<g>
			<path 
				d="	M0,{$start-girth div 2} 
					L{$length},{$end-girth div 2}
					C{$length + $end-girth div 2},{$end-girth div 2} {$length + $end-girth div 2},{-$end-girth div 2} {$length},{-$end-girth div 2}
					L0,{-$start-girth div 2}
					C{-$start-girth div 2},{-$start-girth div 2} {-$start-girth div 2},{$start-girth div 2} 0,{$start-girth div 2}">
				<xsl:attribute name="class">
			        <xsl:apply-templates select="type" mode="to-classes">
					  <xsl:with-param name="postfix">-<xsl:value-of select="@material"/>-<xsl:value-of select="$face"/></xsl:with-param>
					</xsl:apply-templates>
				</xsl:attribute>
			</path>
			<g transform="translate({$length},0)">
				<g transform="scale({1 div $scaleX},1) skewY({-$skewYAngle})">
					<xsl:apply-templates select="part[@socket='joint']" mode="render">
						<xsl:with-param name="state" select="$state"/>
						<xsl:with-param name="face" select="$face"/>
						<xsl:with-param name="orientation" select="$orientation"/>
						<xsl:with-param name="rotation" select="-90"/>
					</xsl:apply-templates>
				</g>
			</g>
			<xsl:apply-templates select="." mode="animate">
				<xsl:with-param name="state" select="$state"/>
			</xsl:apply-templates>											
		</g>
	</xsl:template>

	<xsl:template match="part[type//@name='humanoid-calf']" mode="get-extent">
		<xsl:param name="direction"/>
		<xsl:param name="state"/>
		<xsl:param name="orientation"/>
		<xsl:param name="rotation">0</xsl:param>

		<xsl:apply-templates select="." mode="get-extent-of-socketed-length">
			<xsl:with-param name="direction" select="$direction"/>
			<xsl:with-param name="state" select="$state"/>
			<xsl:with-param name="orientation" select="$orientation"/>
			<xsl:with-param name="rotation" select="$rotation"/>
			<xsl:with-param name="default" select="$humanoid-calf-length-default"/>
			<xsl:with-param name="mult" select="$humanoid-calf-mult"/>
			<xsl:with-param name="socket">none</xsl:with-param>
		</xsl:apply-templates>
	</xsl:template>	

	
	<!-- foot -->
	<xsl:template match="part[type//@name='humanoid-foot']" mode="render-skewed-part">
		<xsl:param name="state"/>
		<xsl:param name="face"/>
		<xsl:param name="skewYAngle">0</xsl:param>
		<xsl:param name="scaleX">1</xsl:param>
		<xsl:param name="orientation"/>
		
		<xsl:variable name="ankle-girth">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-foot-ankle-girth-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">ankle-girth</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-foot-mult"/>
			</xsl:call-template>
		</xsl:variable>

		<xsl:variable name="length">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-foot-length-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">length</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-foot-mult"/>
			</xsl:call-template>
		</xsl:variable>

		<xsl:variable name="height">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-foot-height-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">height</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-foot-mult"/>
			</xsl:call-template>
		</xsl:variable>

		
		
		<polygon points="{-$ankle-girth div 2},0 {$ankle-girth div 2},0 {$length},{$height div 2} {$length},{$height} {-$ankle-girth div 2},{$height}">
			<xsl:attribute name="class">
		        <xsl:apply-templates select="type" mode="to-classes">
				  <xsl:with-param name="postfix">-<xsl:value-of select="@material"/>-<xsl:value-of select="$face"/></xsl:with-param>
				</xsl:apply-templates>
			</xsl:attribute>
		</polygon>
	</xsl:template>
	
	<xsl:template match="part[type//@name='humanoid-foot']" mode="get-extent">
		<xsl:param name="direction"/>
		<xsl:param name="state"/>
		<xsl:param name="orientation"/>
		<xsl:param name="rotation">0</xsl:param>

		<xsl:apply-templates select="." mode="get-extent-of-socketed-length">
			<xsl:with-param name="direction" select="$direction"/>
			<xsl:with-param name="state" select="$state"/>
			<xsl:with-param name="orientation" select="$orientation"/>
			<xsl:with-param name="rotation" select="$rotation"/>
			<xsl:with-param name="default" select="$humanoid-foot-length-default"/>
			<xsl:with-param name="mult" select="$humanoid-foot-mult"/>
		</xsl:apply-templates>
	</xsl:template>	
	

	<!-- neck -->
	<xsl:template match="part[type//@name='humanoid-neck']" mode="render-skewed-part">
		<xsl:param name="state"/>
		<xsl:param name="face"/>
		<xsl:param name="skewYAngle">0</xsl:param>
		<xsl:param name="scaleX">1</xsl:param>
		<xsl:param name="orientation"/>
		
		<xsl:variable name="length">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-neck-length-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">length</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-neck-mult"/>
			</xsl:call-template>
		</xsl:variable>

		<xsl:variable name="girth">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-neck-girth-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">girth</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-neck-mult"/>
			</xsl:call-template>
		</xsl:variable>
		
		<xsl:variable name="head-length">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-head-length-default"/>
				<xsl:with-param name="partNode" select="part[@socket='joint']"/>
				<xsl:with-param name="key">length</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-head-mult"/>
			</xsl:call-template>
		</xsl:variable>

		<xsl:variable name="head-width">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-head-width-default"/>
				<xsl:with-param name="partNode" select="part[@socket='joint']"/>
				<xsl:with-param name="key">width</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-head-mult"/>
			</xsl:call-template>
		</xsl:variable>
		<!-- offset so that the head just sits upon the neck -->
		<xsl:variable name="x" select="$girth * math:abs(number($scaleX)) div 2"/>
		<xsl:variable name="tan" select="$x * math:tan(number($skewYAngle * $degrees-to-radians))"/>
		<xsl:variable name="y" select="($head-length div 2) * math:sqrt(math:abs(number(($x * $x) div (($head-width * $head-width) div 4) - 1)) )"/>
		<xsl:variable name="head-offset" select="$y - $tan"/>
		<!-- 
		<xsl:message>girth = <xsl:value-of select="$girth"/></xsl:message>
		<xsl:message>scaleX = <xsl:value-of select="$scaleX"/></xsl:message>
		<xsl:message>tan = <xsl:value-of select="$tan"/></xsl:message>
		<xsl:message>x = <xsl:value-of select="$x"/></xsl:message>
		<xsl:message>y = <xsl:value-of select="$y"/></xsl:message>
		<xsl:message>head offset = <xsl:value-of select="$head-offset"/></xsl:message>
		 -->
		<polygon points="{$girth div 2},0 {$girth div 2},{-$length} {-$girth div 2},{-$length} {-$girth div 2},0">
			<xsl:attribute name="class">
		        <xsl:apply-templates select="type" mode="to-classes">
				  <xsl:with-param name="postfix">-<xsl:value-of select="@material"/>-<xsl:value-of select="$face"/></xsl:with-param>
				</xsl:apply-templates>
			</xsl:attribute>
		</polygon>		
		
		
		
		<g transform="translate(0,{-$length + $head-length div 2 - $head-offset})">
			<g transform="scale({1 div $scaleX},1) skewY({-$skewYAngle})">
				<!-- head/joint -->
				<xsl:apply-templates select="part[@socket='joint']" mode="render">
					<xsl:with-param name="state" select="state"/>
					<xsl:with-param name="face" select="'outer'"/>
					<xsl:with-param name="orientation" select="$orientation"/>
				</xsl:apply-templates>
			</g>
		</g>
		
	</xsl:template>

	<xsl:template match="part[type//@name='humanoid-neck']" mode="get-extent">
		<xsl:param name="direction"/>
		<xsl:param name="state"/>
		<xsl:param name="orientation"/>
		<xsl:param name="rotation">0</xsl:param>

		<xsl:apply-templates select="." mode="get-extent-of-socketed-length">
			<xsl:with-param name="direction" select="$direction"/>
			<xsl:with-param name="state" select="$state"/>
			<xsl:with-param name="orientation" select="$orientation"/>
			<xsl:with-param name="rotation" select="$rotation"/>
			<xsl:with-param name="default" select="$humanoid-neck-length-default"/>
			<xsl:with-param name="mult" select="$humanoid-neck-mult"/>
		</xsl:apply-templates>
	</xsl:template>	

	<!-- head -->
	<xsl:template match="part[type//@name='humanoid-head']" mode="render">
		<xsl:param name="state"/>
		<xsl:param name="face"/>
		<xsl:param name="orientation"/>
		
		<xsl:variable name="length">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-head-length-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">length</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-head-mult"/>
			</xsl:call-template>
		</xsl:variable>

		<xsl:variable name="width">
			<xsl:call-template name="get-value">
				<xsl:with-param name="default" select="$humanoid-head-width-default"/>
				<xsl:with-param name="partNode" select="."/>
				<xsl:with-param name="key">width</xsl:with-param>
				<xsl:with-param name="mult" select="$humanoid-head-mult"/>
			</xsl:call-template>
		</xsl:variable>
		

		<ellipse rx="{$width div 2}" ry="{$length div 2}" cx="0" cy="{-$length div 2}">
			<xsl:attribute name="class">
		        <xsl:apply-templates select="type" mode="to-classes">
				  <xsl:with-param name="postfix">-<xsl:value-of select="@material"/>-<xsl:value-of select="$face"/></xsl:with-param>
				</xsl:apply-templates>
			</xsl:attribute>		
		</ellipse>
		
	</xsl:template>

	<!-- TODO this appears to be pretty incorrect -->
	<xsl:template match="part[type//@name='humanoid-head']" mode="get-extent">
		<xsl:param name="direction"/>
		<xsl:param name="state"/>
		<xsl:param name="orientation"/>
		<xsl:param name="rotation">0</xsl:param>

		<xsl:apply-templates select="." mode="get-extent-of-socketed-length">
			<xsl:with-param name="direction" select="$direction"/>
			<xsl:with-param name="state" select="$state"/>
			<xsl:with-param name="orientation" select="$orientation"/>
			<xsl:with-param name="rotation" select="$rotation"/>
			<xsl:with-param name="default" select="$humanoid-head-length-default"/>
			<xsl:with-param name="mult" select="$humanoid-head-mult"/>
		</xsl:apply-templates>
	</xsl:template>	

</xsl:stylesheet>